//
//  ViewController.m
//  CollectionViewTemplate
//
//  Created by pi on 15/11/16.
//  Copyright (c) 2015年 pi. All rights reserved.
//

#import "ViewController.h"
#import "PiCollectionViewCell.h"
#import "PiCollectionHeaderView.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *array;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(NSMutableArray *)array{
    if(!_array){
        _array=[[NSMutableArray alloc]initWithCapacity:10];
    }
    return _array;
}

- (IBAction)addNewItem:(UIBarButtonItem *)sender {
    NSInteger insertCount=10;
    NSInteger insertStartIndex=self.array.count-1;
    for(NSInteger i=0;i<insertCount;i++){
        [self.array addObject:[NSString stringWithFormat:@"%ld",i]];
    }
    if(self.collectionView.numberOfSections==0){
        [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:0]];
    }else{
        [self.collectionView performBatchUpdates:^{
            for(NSInteger i=insertStartIndex ;i<insertStartIndex+insertStartIndex;i++){
                [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:i inSection:0]]];
            }
        } completion:^(BOOL finished) {
            
        }];
    }
    
}

- (IBAction)segmentChanged:(UISegmentedControl *)sender {
}


#pragma mark - UICollectionViewDataSource
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PiCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    [cell.label setTitle:self.array[indexPath.item] forState:UIControlStateNormal];
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.array.count==0 ? 0 :1;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    PiCollectionHeaderView *header= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Supplementary" forIndexPath:indexPath];
    return header;
}

#pragma mark - UICollectionViewDelegateFlowLayout
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    
//}
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    
//}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"selected : %@",indexPath.item);
}
@end
